package com.naiterui.ehp.bs.b2c.product.repository;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bs.b2c.product.entity.Sku;

/**
 * 商品规格
 *
 * @author zhoupeipei
 * @date 2019-05-17 16:14
 * @since 1.0.0
 */
@Repository
public interface ISkuRepository extends BaseJpaRepository<Sku, Long> {

    /**
     * 批量获取sku
     *
     * @param skuIds
     *
     * @return
     */
    List<Sku> findAllByIdIn(Set<Long> skuIds);

    /**
     * 根据分类获取sku列表
     *
     * @param categoryId
     *
     * @return
     */
    @Query(
        value = "select s from Sku s join Medicine m on s.productId=m.productId where m.categoryId=:categoryId and s.status=:status and s.defaultSku=0 ORDER BY "
            +
            "s.createdAt desc")
    Page<Sku> findSkuByCategoryIdAndStatusPageable(@Param("categoryId") Long categoryId, @Param("status") int status, Pageable pageable);

    /**
     * 根据关键字名称模糊匹配查找商品
     *
     * @param name
     *
     * @return
     */
    Page<Sku> findByNameLikeAndStatusAndDefaultSku(@Param("name") String name, @Param("status") int status,
        @Param("default_sku") int defaultSku, Pageable pageable);

    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE Sku SET defaultSku= 1 WHERE productId= :productId")
    void updateNotDefatuleSku(@Param("productId") Long productId);

    List<Sku> findByProductId(@Param("productId") Long productId);

    List<Sku> findAllByIdIn(Collection<Long> skuIds);

    /**
     * 获取全部上架商品
     * @param status
     * @param pageable
     * @return
     */
    Page<Sku> findByStatusAndDefaultSku(@Param("status") int status, @Param("default_sku") int defaultSku,
        Pageable pageable);

    /**
     * 根据商品id获取sku
     */
    List<Sku> findByProductIdIn(Set<Long> productIdSet);

    /**
     * 根据医生id获取常用药品
     *
     * @param doctorId
     *            医生id
     * @param pageable
     *            分页信息
     * @return
     */
    @Query(
        value = "select s from CommonDrug cd left join Sku s on s.productId=cd.productId where cd.doctorId=:doctorId and s.defaultSku=0 ORDER BY cd.createdAt desc")
    Page<Sku> findSkuByDoctorIdPageable(@Param("doctorId") Long doctorId, Pageable pageable);

    /**
     * 根据医生id获取常用药品(不包含下架的)
     * 
     * @param status
     *            状态 0.下架 1.正常
     * @param doctorId
     *            医生id
     * @param pageable
     *            分页信息
     * @return
     */
    @Query(
        value = "select s from CommonDrug cd left join Sku s on s.productId=cd.productId where s.status=:status and cd.doctorId=:doctorId and s.defaultSku=0 ORDER BY cd.createdAt desc")
    Page<Sku> findSkuByStatusAndDoctorIdPageable(@Param("status") int status, @Param("doctorId") Long doctorId,
        Pageable pageable);
}
